package crud;


import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 孙继峰
 * @date 2019/06/17
 */
public class Test12 {

    private static final String IP = "10.15.15.89";
    private static final String HTTP_PROTOCOL = "http://";

    /**
     * 对 url 中用户名进行过滤
     *
     * @param url url
     * @return 不含用户名的 url
     */
    public String filter(String url) {
        int startIndex = url.indexOf(HTTP_PROTOCOL) + HTTP_PROTOCOL.length();
        int endIndex = url.indexOf(IP);

        return new StringBuilder()
                .append(url.substring(0, startIndex))
                .append(url.substring(endIndex))
                .toString();
    }

    @Test
    public void testFilter() {
        Assert.assertEquals("http://10.15.15.89:8090/demo/testWar.git",
                filter("http://sysadmin:123456@10.15.15.89:8090/demo/testWar.git"));
    }
}

class Test1 {
    // 题目没太看懂... 按照我个人理解去做了
    /**
     * 自增主键
     */
    private AtomicInteger key = new AtomicInteger(0);

    /**
     * 向 db 中插入一条数据
     *
     * @param name column 的值
     */
    public void insert(String name) {
        PreparedStatement sta = null;
        String sql = "INSERT INTO UNIQUE_TABLE VALUES(?,?)";
        try {
            sta = getConnection().prepareStatement(sql);
            sta.setString(1, name);
            sta.setInt(2, key.getAndIncrement());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 Connection
     * 1.7 以后 Connection 实现 AutoCloseable 接口, 不需要手动关闭
     *
     * @return connection
     */
    public Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "000000");
        } catch (ClassNotFoundException | SQLException e) {
            // TODO: Logger
            e.printStackTrace();
        }
        return con;
    }
}

class Test2 implements Filter {

    private static final String HEADER_AUTHORIZATION = "Authorization";

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Token token = parseToken(request.getHeader(HEADER_AUTHORIZATION));
        if (isValid(token)) {
            return;
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    /**
     * 判断 Token 是否失效
     *
     * @return 失效时 true, 否则 false
     */
    public boolean isValid(Token token) {
        Date now = new Date();
        Date invalidDate = new Date(token.getInvalidDate());
        return now.after(invalidDate);
    }

    /**
     * 解析 token 字符串
     *
     * @param token token 字符串
     * @return 解析后的 Token 对象
     * @throws IllegalArgumentException 当 token 格式不正确时抛出
     */
    public Token parseToken(String token) throws IllegalArgumentException {
        String[] data = token.split(".");
        int id = 0;
        long invalidDate = 0L;
        try {
            id = Integer.parseInt(data[0]);
            invalidDate = Long.parseLong(data[1]);
        } catch (ArrayIndexOutOfBoundsException e) {
            // TODO: Logger
            e.printStackTrace();
        }
        return new Token(id, invalidDate);
    }

    @Data
    @AllArgsConstructor
    class Token {
        /**
         * 唯一标识
         */
        private int id;
        /**
         * 假设是以毫秒形式存储吧
         */
        private long invalidDate;
    }

    private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("拆分线程-%d").build();
    private ExecutorService executorService = new ThreadPoolExecutor(4, 4,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024),
            namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    /**
     * @param fileName
     * @param count
     */
    public void split(String fileName, int count) {

    }
}
